﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDIDB;

namespace PowerControl
{
    public partial class SimulationForm : Form
    {
        private Simulation sim;
        private DBGraphics memGraphics;

        public SimulationForm()
        {
            //YO DAWG
            InitializeComponent();
        }

        private void SimulationForm_Load(object sender, EventArgs e)
        {
            memGraphics = new DBGraphics();
            memGraphics.CreateDoubleBuffer(pictureBox1.CreateGraphics(), pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
        }

        private void newSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Is there a simulation running ?
            if (sim == null)
            {
                //if not, create a new one
                sim = new Simulation();
                sim.DrawSimulation(memGraphics.g, pictureBox1.Width, pictureBox1.Height);
                memGraphics.Render(pictureBox1.CreateGraphics());
                this.toolStripStatusLabel1.Text = "READY";
            }
            else
                Utils.ErrMsg("There is already a simulation loaded/running");
        }

        private void simTimer_Tick(object sender, EventArgs e)
        {
            //Do one step of the simulation
            sim.Step((double)(simTimer.Interval)/1000.0);

            //Draw the simulation
            sim.DrawSimulation(memGraphics.g, pictureBox1.Width, pictureBox1.Height);
            memGraphics.Render(pictureBox1.CreateGraphics());
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sim != null)
            {
                this.simTimer.Enabled = true;
                this.toolStripStatusLabel1.Text = "RUNNING";
                //
                sim.MinX = -(double)(pictureBox1.Width / 2);
                sim.MinY = -(double)(pictureBox1.Height / 2);
                sim.MaxX = (double)(pictureBox1.Width / 2);
                sim.MaxY = (double)(pictureBox1.Height / 2);
                //
            }
            else
                Utils.ErrMsg("There is no simulation loaded");
        }

        private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sim != null)
            {
                this.simTimer.Enabled = false;
                this.toolStripStatusLabel1.Text = "PAUSED";
            }
            else
                Utils.ErrMsg("There is no simulation loaded/running");
        }

        private void pauseToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void antennaToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void loadSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Simulation file loading
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckPathExists = true;
            dlg.CheckFileExists = true;
            dlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                sim = Simulation.FromXML(dlg.FileName);
                sim.DrawSimulation(memGraphics.g, pictureBox1.Width, pictureBox1.Height);
                memGraphics.Render(pictureBox1.CreateGraphics());
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        private void displayInformationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MobileInfo pol = new MobileInfo(this.sim);
            pol.Show();
        }

        private void radiationPatternToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RadiationForm pol = new RadiationForm(this.sim);
            pol.Show();
        }

        private void SimulationForm_SizeChanged(object sender, EventArgs e)
        {
            sim.MinX = -(double)(pictureBox1.Width / 2);
            sim.MinY = -(double)(pictureBox1.Height / 2);
            sim.MaxX = (double)(pictureBox1.Width / 2);
            sim.MaxY = (double)(pictureBox1.Height / 2);
            memGraphics.CreateDoubleBuffer(pictureBox1.CreateGraphics(), pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
        }

        private void SimulationForm_Resize(object sender, EventArgs e)
        {
        }
    }
}
